package com.raspositos.guitarfrik.android;

import java.util.Random;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.provider.MediaStore.Audio.AudioColumns;


public class Prefs {
	
	public static final String PREF_NAME = "com.raspositos.guitarfrik.android_preferences";
	private static Prefs instance;
	private SharedPreferences shp;
	private enum enmPrefs {INTRO_HITS, INTRO_TYPE, INTRO_REF, VIBRATE, DEFAULT_SORT, DEFAULT_FILTER, ART_SIZE, FIRSTTIME, MUSIC_STORE, INTRO_SECS};
	public enum IntroTypes { SYSTEM, BUILT_IN, RINGTONE }
	public enum SoundFx { 
		KEY_CLICK(R.string.fx_key_click,AudioManager.FX_KEY_CLICK),
		NAV_UP(R.string.fx_nav_up, AudioManager.FX_FOCUS_NAVIGATION_UP),
		NAV_DN(R.string.fx_nav_dn, AudioManager.FX_FOCUS_NAVIGATION_DOWN),
		NAV_LF(R.string.fx_nav_lf, AudioManager.FX_FOCUS_NAVIGATION_LEFT),
		NAV_RI(R.string.fx_nav_ri, AudioManager.FX_FOCUS_NAVIGATION_RIGHT),
		KEY_STD(R.string.fx_nav_std, AudioManager.FX_KEYPRESS_STANDARD),
		KEY_SPC(R.string.fx_nav_spc, AudioManager.FX_KEYPRESS_SPACEBAR),
		KEY_DEL(R.string.fx_nav_del, AudioManager.FX_KEYPRESS_DELETE),
		KEY_RET(R.string.fx_nav_ret, AudioManager.FX_KEYPRESS_RETURN);
		
		private int id;
		private int string;

		SoundFx(int str, int id){
			this.string = str;
			this.id = id;
		}

		public final int getId() {
			return id;
		}

		public final int getString() {
			return string;
		}
	}
	
	public enum SongSort {
		ARTIST(AudioColumns.ARTIST + " ASC ", R.string.sort_artist),
		ALBUM(AudioColumns.ALBUM + " ASC", R.string.sort_album),
		SONG(AudioColumns.TITLE + " ASC", R.string.sort_song);
		
		private String orderBy;
		private int strId;
		SongSort(String orderBy, int sid){
			this.orderBy = orderBy;
			this.strId = sid;
		}
		
		/**
		 * @return the strId
		 */
		public final int getStringId() {
			return strId;
		}
		public static SongSort findBySort(String sortBy) {
			SongSort ss = ARTIST;
			SongSort[] values = SongSort.values();
			for (int i = 0; i < values.length; i++) {
				SongSort el = values[i];
				if (el.orderBy.equals(sortBy)){
					ss = el;
					break;
				}
			}
			return ss;
		}
	}
	
	public enum SongFilter {
		ALL("", R.string.flt_all),
		SONG(AudioColumns.TITLE , R.string.flt_song),
		ARTIST(AudioColumns.ARTIST, R.string.flt_artist);
		
		private String filter;
		private int strId;
		SongFilter(String filter, int sid){
			this.filter = filter;
			this.strId = sid;
		}
		/**
		 * @return the strId
		 */
		public final int getStringId() {
			return strId;
		}
		public static SongFilter findFilter(String flt) {
			SongFilter ss = ALL;
			SongFilter[] values = SongFilter.values();
			for (int i = 1; i < values.length; i++) {
				SongFilter el = values[i];
				if (flt.startsWith(el.filter)){
					ss = el;
					break;
				}
			}
			return ss;
		}
		public String getFilter() {
			return filter;
		}
	}
	
	public enum BuiltInIntros{
		Sticks("Sticks", R.raw.baquetas ),
		Snap("Finger Snap", R.raw.chasquido ),
		HitHat("Hit-Hat", R.raw.hithat ),
		Wood("Wood", R.raw.madera ),
		Tamb("Tambourine", R.raw.pandero ),
		Rumble("Rumble", R.raw.redoble ),
		CTraingle("Closed Triangle", R.raw.traingulo_cerrado ),
		OTriangle("Open Traingle", R.raw.triangulo ),
		Unknown("Unknown", R.raw.baquetas);
		
		private int resource;
		private String name;
		BuiltInIntros(String nam, int id){
			name = nam;
			resource = id;
		}
		
		private static BuiltInIntros fromName(String name){
			BuiltInIntros sec = Unknown;
			name = (name + "").toLowerCase();
			for (int i = 0; i < BuiltInIntros.values().length; i++) {
				BuiltInIntros test = BuiltInIntros.values()[i];
				if (name.equalsIgnoreCase(test.name)){
					sec = test;
					break;
				}
			}
			return sec;
		}

		public String toFileName() {
			return resource + ".mp3";
		}

		public int getResource() {
			return resource;
		}
	}

	private Prefs (Context ctxt){
		shp = ctxt.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
		if (! shp.contains(enmPrefs.INTRO_HITS.toString())){
			init(ctxt);
		}
	}

	private void init(Context ctxt) {
		Editor edit = shp.edit();
		edit.putString(enmPrefs.INTRO_HITS.toString(), "3");//3 hits by default
		edit.putString(enmPrefs.INTRO_TYPE.toString(), IntroTypes.BUILT_IN.toString()); //Built-in
		edit.putString(enmPrefs.INTRO_REF.toString(), getDefaultIntroRef());//Hit Hat
		edit.putString(enmPrefs.INTRO_SECS.toString(), "1");//1 sec grace by default
		edit.putBoolean(enmPrefs.VIBRATE.toString(), true);//Vibrate
		edit.putString(enmPrefs.DEFAULT_SORT.toString(), SongSort.ARTIST.orderBy);//Sort by Artist
		edit.putString(enmPrefs.DEFAULT_FILTER.toString(), SongFilter.ALL.filter);//No Filter
		edit.putString(enmPrefs.ART_SIZE.toString(), "96");//Medium (192/96/48)
		edit.putBoolean(enmPrefs.FIRSTTIME.toString(), true);//Medium (192/96/48)
		String[] affiliates = ctxt.getResources().getStringArray(R.array.affiliate_value);
		edit.putString(enmPrefs.MUSIC_STORE.toString(), affiliates[new Random().nextInt(affiliates.length)]);
		edit.commit();
	}

	public static synchronized Prefs getPrefs(Context c){
		if (instance == null){
			instance = new Prefs(c);
		}
		return instance;
	}

	public String getMusicStore(){
		return shp.getString(enmPrefs.MUSIC_STORE.toString(), "");
	}
	
	public int getIntroHits(){
		return Integer.parseInt(shp.getString(enmPrefs.INTRO_HITS.toString(), "3"));
	}
	
	public int getIntroMiliSecs(){
		return Integer.parseInt(shp.getString(enmPrefs.INTRO_SECS.toString(), "0")) * 1000;
	}
	
	public IntroTypes getIntroType(){
		return IntroTypes.valueOf(shp.getString(enmPrefs.INTRO_TYPE.toString(), IntroTypes.BUILT_IN.toString()));
	}
	
	public String getIntroRef(){
		String introRef = shp.getString(enmPrefs.INTRO_REF.toString(), getDefaultIntroRef());
//		Log.d("GuitarFrik", "IntroRef:" + introRef);
		BuiltInIntros intro = BuiltInIntros.fromName(introRef);
//		Log.d("GuitarFrik", "Introenum:" + intro);
		//if (intro != BuiltInIntros.Unknown){
			introRef = intro.toFileName();
		//}
//		Log.d("GuitarFrik", "IntroRef:" + introRef);
		return introRef;
	}
	
	public String getDefaultIntroRef(){
		return BuiltInIntros.HitHat.name + "";
	}
	
	public boolean getVibrate(){
		return shp.getBoolean(enmPrefs.VIBRATE.toString(), true);
	}
	
	public String getSortBy(){
		return shp.getString(enmPrefs.DEFAULT_SORT.toString(), SongSort.ARTIST.orderBy);
	}
	
	public SongSort getSongSort(){
		return SongSort.findBySort(getSortBy());
	}
	
	public String getFilter(){
		return shp.getString(enmPrefs.DEFAULT_FILTER.toString(), SongFilter.ALL.filter);
	}
	
	public int getArtSize(){
		return Integer.parseInt(shp.getString(enmPrefs.ART_SIZE.toString(), "96"));
	}

	public void setSongSort(int pos) {
		Editor edit = shp.edit();
		edit.putString(enmPrefs.DEFAULT_SORT.toString(), SongSort.values()[pos].orderBy);
		edit.commit();
	}

	public void setFilter(SongFilter filter, String value) {
		Editor edit = shp.edit();
		if (filter == SongFilter.ARTIST){
			edit.putString(enmPrefs.DEFAULT_FILTER.toString(), filter.filter + " = '" + value + "'");
		} else if (filter == SongFilter.ALL){
			edit.putString(enmPrefs.DEFAULT_FILTER.toString(), filter.filter);
		} else {//SONG
			edit.putString(enmPrefs.DEFAULT_FILTER.toString(), filter.filter + " LIKE '" + value + "%'");
		}
		edit.commit();
	}

	public boolean isFirstTime() {
		return shp.getBoolean(enmPrefs.FIRSTTIME.toString(), true);
	}

	public void setNotFirstTime() {
		Editor edit = shp.edit();
		edit.putBoolean(enmPrefs.FIRSTTIME.toString(), false);
		edit.commit();
	}
	
}
